//
//  ZYBaseController.m
//  Beautiful
//
//  Created by admin on 2017/5/2.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "ZYBaseController.h"
@interface ZYBaseController ()
@property (nonatomic, strong) UIImageView *imageViewNoData;

@property (nonatomic, strong) UILabel *labelNoData;
@property (nonatomic ,strong)UIView *popView;

@property (nonatomic,strong)UILabel *labelText;

@end

@implementation ZYBaseController

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self hideLoading];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//     [[UIApplication sharedApplication] setStatusBarStyle:(UIStatusBarStyleDefault)];
    
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.view.width = kScreenWidth;
    self.view.height = kScreenHeight;
  // WEAKSELF
    [self.view layoutIfNeeded];
    
    _contentWidth = kScreenWidth;
    _contentHeight = kScreenHeight - (STATUSBAR_HEIGHT+44);
   
    
    
    
    _mbp = (MBProgressHUD *)[[APPDELEGATE window] viewWithTag:99999];
    //    _mbp.opacity = 0.6f;
    //    _mbp.tag = 99999;
    //    [[APPDELEGATE window] addSubview:_mbp];
//    [[[[UIApplication sharedApplication] delegate] window] viewWithTag:99999];
    
    if (!_mbp) {
        _mbp = [[MBProgressHUD alloc] initWithView:[APPDELEGATE window]];
        //_mbp.opacity = 0.6f;
        _mbp.tag = 99999;
        _mbp.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        _mbp.bezelView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.4];
        _mbp.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
        _mbp.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.4];
        _mbp.contentColor = [UIColor whiteColor];
        [[APPDELEGATE window] addSubview:_mbp];
    }
//    if (!_mbp) {
//        _mbp = [[MBProgressHUD alloc] initWithView:[APPDELEGATE window]];
//        _mbp.opacity = 0.6f;
//        _mbp.tag = 99999;
//        [[APPDELEGATE window] addSubview:_mbp];
//    }
    self.view.backgroundColor = kColorf2f6f7;
    
    [self.view addSubview:self.labelNoData];
    [self.view addSubview:self.imageViewNoData];
    self.labelNoData.hidden = YES;
    self.imageViewNoData.hidden = YES;
    
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideLoading) name:@"hiddenLoading" object:nil];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.imageViewNoData.frame = CGRectMake((kScreenWidth - 150) / 2, 216.5, 150, 150);
    self.labelNoData.frame = CGRectMake(0, self.imageViewNoData.bottom + 7, kScreenWidth, self.labelNoData.font.lineHeight);
}
- (void)toast:(NSString *)text
{
    _mbp.mode = MBProgressHUDModeText;
    //    _mbp.delegate = [[[UIApplication sharedApplication] delegate] window];
    //_mbp.labelText = text;
    _mbp.detailsLabel.text = text;
    _mbp.margin = 15.f;
    _mbp.detailsLabel.font = kFontSystem(16);
    _mbp.userInteractionEnabled = NO;
    _mbp.minShowTime = 1.0;
    [[APPDELEGATE window] bringSubviewToFront:_mbp];
    [_mbp showAnimated:YES];
    [_mbp hideAnimated:YES afterDelay:0.6];
}

- (void)showLoading:(NSString *)text
{
    _mbp.detailsLabel.text = @"";
    _mbp.mode = MBProgressHUDModeIndeterminate;
    //    _mbp.delegate = self;
    //    [_mbp setColor:[UIColor clearColor]];
    if (text) {
        _mbp.label.text = text;
    }else{
        _mbp.label.text = @"";
    }
    _mbp.margin = 20.f;
   // _mbp.userInteractionEnabled = YES;
    _mbp.detailsLabel.font = kFontSystem(16);
    _mbp.minShowTime = 0;
    [[APPDELEGATE window] bringSubviewToFront:_mbp];
    [_mbp showAnimated:YES];
}

- (void)hideLoading {
    _mbp.label.text = @"";
    _mbp.detailsLabel.text = @"";
    [self.mbp hideAnimated:YES];
    [_mbp hideAnimated:YES afterDelay:0];
}


- (void)initTopBarWithTitle:(NSString *)title{
    [self.titleLabel setText:title];
    self.topBar.backgroundColor=[UIColor hexStringToColor:@"#fafafa" alpha:1.0];
    [self.topBar addSubview:self.titleLabel];
    [self.view addSubview:self.topBar];
}

- (void)initTopBarWithImage:(UIImage *)image width:(float)imageWidth height:(float)imageHeight
{
    imageWidth = imageWidth? imageWidth:image.size.width/2;
    imageHeight = imageHeight? imageHeight:image.size.height/2;
    UIImageView *titleImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, STATUSBAR_HEIGHT, imageWidth, imageHeight)];
    [titleImage setCenter:CGPointMake(_contentWidth/2, STATUSBAR_HEIGHT+22)];
    [titleImage setImage:image];
    [self.topBar addSubview:titleImage];
     self.topBar.backgroundColor=[UIColor hexStringToColor:@"#fafafa" alpha:1.0];
    [self.view addSubview:self.topBar];
}
-(void)initTopBarWithView:(UIView *)view
{
    
    [self.topBar addSubview:view];
    [self.view addSubview:self.topBar];
}






- (void)addLeftButton:(UIImage *)leftButtonImage width:(float)imageWidth height:(float)imageHeight
{
    //    imageWidth = imageWidth? imageWidth:leftButtonImage.size.width/2;
    //    imageHeight = imageHeight? imageHeight:leftButtonImage.size.height/2;
    _leftButton = [[UIButton alloc]initWithFrame:CGRectMake(1, kDevice_Is_iPhoneX?44:20, 56, 44)];
    //    UIImageView *leftImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, imageWidth, imageHeight)];
    //    [leftImg setImage:leftButtonImage];
    //    [leftImg setCenter:CGPointMake(28, 22)];
    if (leftButtonImage) {
        [_leftButton setImage:leftButtonImage forState:UIControlStateNormal];
    }
    //    [_leftButton setTitle:@"返回" forState:UIControlStateNormal];
    [_leftButton setTitleColor:kColor333333 forState:UIControlStateNormal];
    _leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
    [_leftButton addTarget:self action:@selector(leftButtonPress) forControlEvents:UIControlEventTouchUpInside];
    [_leftButton.titleLabel setFont:kFontSystem(16)];
    
    [self.topBar addSubview:_leftButton];
}

- (void)addRightButton:(UIImage *)rightButtonImage width:(float)imageWidth height:(float)imageHeight
{
    
    _rightButton = [[UIButton alloc]initWithFrame:CGRectMake(_contentWidth-57, kDevice_Is_iPhoneX?44:20, 56, 44)];
    if (rightButtonImage) {
        [_rightButton setImage:rightButtonImage forState:UIControlStateNormal];
    }
    [_rightButton.titleLabel setFont:kFontBoldSystem(16)];
    
    _rightButton.tag = 2000;
    [_rightButton addTarget:self action:@selector(rightButtonPress) forControlEvents:UIControlEventTouchUpInside];
    
    [self.topBar addSubview:_rightButton];
}

- (void)addRightButtonText:(NSString *)rightButtonText width:(float)imageWidth height:(float)imageHeight
{
    _rightButton = [[UIButton alloc]initWithFrame:CGRectMake(_contentWidth-90, kDevice_Is_iPhoneX?44:20,100, 44)];
    [_rightButton setTitle:rightButtonText forState:UIControlStateNormal];
    [_rightButton setTitleColor:KItemColor forState:UIControlStateNormal];
    [_rightButton.titleLabel setFont:kFontSystem(16)];
    _rightButton.titleLabel.textAlignment=NSTextAlignmentRight;
    _rightButton.tag = 2000;
    [_rightButton addTarget:self action:@selector(rightButtonPress) forControlEvents:UIControlEventTouchUpInside];
    
    [self.topBar addSubview:_rightButton];
}
- (void)addLeftButtonText:(NSString *)rightButtonText width:(float)imageWidth height:(float)imageHeight
{
    _leftButton = [[UIButton alloc]initWithFrame:CGRectMake(1, kDevice_Is_iPhoneX?44:20, 56, 44)];
    [_leftButton setTitle:rightButtonText forState:UIControlStateNormal];
    [_leftButton setTitleColor:KItemColor forState:UIControlStateNormal];
    [_leftButton.titleLabel setFont:kFontSystem(16)];
    
  //  _rightButton.tag = 2000;
    [_leftButton addTarget:self action:@selector(leftButtonPress) forControlEvents:UIControlEventTouchUpInside];
    
    [self.topBar addSubview:_leftButton];
}
- (void)setupUI
{
    
}
// 默认左边按钮为返回功能，若其他功能，子类中重写此段代码
- (void)leftButtonPress
{
    WEAKSELF
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    });
}

// 右边按钮点击事件，子类中重写此段代码
- (void)rightButtonPress
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma  UI

- (UIView *)topBar {
    
    if (!_topBar) {
        _topBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _contentWidth, NavHeight)];
        
        NSLog(@"%f-----%f",(44+STATUSBAR_HEIGHT),NavHeight);
        
        
       // [_topBar setBackgroundColor:[UIColor colorWithHexString:@"ff7870"]];
        [_topBar setBackgroundColor:[UIColor whiteColor]];
        UIView *viewTopLine = [[UIView alloc] initWithFrame:CGRectMake(0, _topBar.bottom - 1, _topBar.width, 0.5)];
        viewTopLine.backgroundColor = [UIColor colorWithHexString:@"#C0C0C0"];
      //  [_topBar addSubview:viewTopLine];
        
        
        UIView *viewBottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, _topBar.bottom - 0.5, _topBar.width, 0.5)];
        self.viewBottomLine = viewBottomLine;
        viewBottomLine.backgroundColor = [UIColor colorWithHexString:@"#FAFAFA"];
        [_topBar addSubview:viewBottomLine];
    }
    return _topBar;
}

- (UILabel *)titleLabel {
    
    if (!_titleLabel) {
        
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, kDevice_Is_iPhoneX?44:20, kScreenWidth-120, 40)];
        _titleLabel.font = kFontSystem(18);
        _titleLabel.backgroundColor = [UIColor clearColor];
        [_titleLabel setTextColor:KTitleColor];
        [_titleLabel setTextAlignment:NSTextAlignmentCenter];
    }
    return _titleLabel;
}


- (NSString *)controllerTitle {
    
    return nil;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    
}

- (void)viewDidDisappear:(BOOL)animated {
    
    [super viewDidDisappear:animated];
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"hiddenLoading" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 无数据相关
- (void)setNoDataViweWithTitle:(NSString *)title img:(UIImage *)img {
    self.imageViewNoData.image = img;
    self.labelNoData.text = title;
}

- (void)showNoDataView {
    [self.view bringSubviewToFront:self.imageViewNoData];
    [self.view bringSubviewToFront:self.labelNoData];
    self.imageViewNoData.hidden = NO;
    self.labelNoData.hidden = NO;
}

- (void)hiddenNoDataView {
    self.imageViewNoData.hidden = YES;
    self.labelNoData.hidden = YES;
    [self.view sendSubviewToBack:self.imageViewNoData];
    [self.view sendSubviewToBack:self.labelNoData];
}

- (UILabel *)labelNoData {
    if (_labelNoData == nil) {
        _labelNoData = [[UILabel alloc] init];
        _labelNoData.backgroundColor = [UIColor clearColor];
        _labelNoData.font = [UIFont systemFontOfSize:12];
        _labelNoData.textColor = [UIColor colorWithHexString:@"#a3a3a3"];
        _labelNoData.textAlignment = NSTextAlignmentCenter;
    }
    return _labelNoData;
}

- (UIImageView *)imageViewNoData {
    if (_imageViewNoData == nil) {
        _imageViewNoData = [[UIImageView alloc] init];
    }
    return _imageViewNoData;
}
-(void)showPopViewWithTitle:(NSString *)title
{
    self.labelText.text=title;
    [self.popView addSubview:self.labelText];
    [self.maskView addSubview:self.labelText];
    [self.labelText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(_maskView);
    }];
    [[UIApplication sharedApplication].keyWindow addSubview:self.maskView];
 //   [[UIApplication sharedApplication].keyWindow addSubview:self.popView];
}
-(void)hiddenPopView
{
    [self.maskView removeFromSuperview];
    [self.popView removeFromSuperview];
}
- (UIView *)popView {
    if (!_popView) {
        _popView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        _popView = [[UIView alloc] initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.height/2-120, [UIScreen mainScreen].bounds.size.width-20, 230*DUP)];
        _popView.layer.cornerRadius=10.0f;
        _popView.backgroundColor=[UIColor whiteColor];
        _popView.clipsToBounds=YES;
        //self.quxiao.center=self.quxiao.titleLabel.center;
        
    }
    
    return _popView;
}
- (UIView *)maskView {
    if (!_maskView) {
        _maskView = [[UIView alloc] initWithFrame:SCREEN_BOUNDS];
        _maskView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
       // _maskView.alpha = 0.5f;
    }
    return _maskView;
}
-(UILabel *)labelText
{
    if (!_labelText) {
        _labelText=[[UILabel alloc]init];
        _labelText.textColor=[UIColor whiteColor];
        _labelText.font=[UIFont systemFontOfSize:25*DUP];
        _labelText.textAlignment=NSTextAlignmentCenter;
        _labelText.contentMode=UIViewContentModeScaleAspectFit;
    }
    return _labelText;
}

-(CGFloat)navHeight
{
    CGFloat height = 64;
    if (kDevice_Is_iPhoneX) {
        height = 88;
    }
    return height;
}
-(CGFloat)tabHeight
{
    CGFloat height = 44;
    if (kDevice_Is_iPhoneX) {
        height=88;
    }
    return height;
}










@end
